//devices.js
const {jump,toast,getPageInstance} = require('../../utils/util');
const {bluetoothAdapterState} = require('../../utils/bluetooth');
const {Event} = require('../../utils/constant');
const app = getApp();
Page({
  data: {},
  onLoad: function () {
    const that = this;
    app.event.on(Event.BluetoothAdapterStateChange, that.BluetoothAdapterStateChangeHandler, that);

  },
  onUnload:function(){
    app.event.off(Event.BluetoothAdapterStateChange, this.BluetoothAdapterStateChangeHandler);
  },
  BluetoothAdapterStateChangeHandler:function(){
    this.opened();
  },
  opened: function () {

    wx.getBluetoothAdapterState({
      success:function (res) {
        const {discovering,available} = res;
        if(!available){
          wx.showToast({title: '请打开蓝牙', icon: 'none'});
        }else{
          let pageInstance = getPageInstance('searchBySerialNumber');
          if(pageInstance){
            wx.navigateBack();
          }else{
            jump({pageName:'devices'});
          }

        }

      },
      fail:function (res) {
        wx.showToast({title: '请打开蓝牙', icon: 'none'});
      }
    });


  }
});
